import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  routes
})

//判断权限
function isAuth(to, form, next) {
  let token = store.state.user.token
  if (token && token != '') {
    if (to.matched.length === 0) {  //如果未匹配到路由
      form.name ? next() : router.replace('/notFind')
    } else {
      document.title = to.meta.title
      next()
    }
  } else {
    router.replace({ path: '/login' })
  }
}

router.beforeEach((to, form, next) => {
  if (to.path != '/login') {
    isAuth(to, form, next)
  } else {
    document.title = to.meta.title;
    next()
  }
})

export default router
